package com.neusoft.demo.controller;

import com.neusoft.demo.entity.Admin;
import com.neusoft.demo.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * (Admin)表控制层
 *
 * @author makejava
 * @since 2020-05-27 11:15:45
 */
@Controller
@RequestMapping("admin")
public class AdminController {
    /**
     * 服务对象
     */
    @Resource
    private AdminService adminService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     * PreparedStatement防止sql注入攻击
     */
    @GetMapping("getById")
    @ResponseBody
    public Admin selectOne(Integer id) {
        return this.adminService.queryById(id);
    }

    @RequestMapping("toLogin")
    public String tologin(){
        return "login";
    }

    @RequestMapping("toWelcome")
    public String toWelcome(){
        return "/page/welcome-1";
    }

    @RequestMapping("index")
    public String index(){ return "index";}

    @RequestMapping("login")
    public String login(String account, String password, Model model, HttpSession session){
        Admin admin = adminService.login(account,password);
        if(admin == null) {
            session.invalidate();   //登录失败，清空session
            model.addAttribute("msg","账号或密码错误");
            return "login";
        }

        //使用Cookie记录账号密码
        //将用户信息存放到session

        session.setAttribute("account", account);   //登录成功，session记录账号

        return "index";
    }

    @GetMapping("toIndex")
    public String toIndex(){
        return "index";
    }

    @PostMapping("insert")
    @ResponseBody
    public Admin insert(Admin bean){
        return  adminService.insert(bean);
    }

}